package cn.itcast.demo;

import cn.itcast.demo.pojo.Heima55;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.junit.Test;

import java.util.List;
import java.util.Map;

public class SolrSuperQueryIndexTest {


    @Test
    public void testRangeQueryIndex() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //封装查询条件对象,范围查询，范围查询包含左右边界
        SolrQuery query =new SolrQuery("price:[* TO 149999]");

        //执行查询，传入查询条件，并执行查询得到响应
        QueryResponse queryResponse = solrServer.query(query);

        //解析查询的结果
        SolrDocumentList results = queryResponse.getResults();

        results.forEach(result->{
            String id = (String) result.get("id");
            String title = (String) result.get("title");
            Long price = Long.valueOf(result.get("price").toString());

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });

    }



    //AND  OR   NOT
    @Test
    public void testBooleanQueryIndex() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //封装查询条件对象,范围查询，范围查询包含左右边界
        SolrQuery query =new SolrQuery("price:[* TO 149999] AND title:小米");

        //执行查询，传入查询条件，并执行查询得到响应
        QueryResponse queryResponse = solrServer.query(query);

        //解析查询的结果
        SolrDocumentList results = queryResponse.getResults();

        results.forEach(result->{
            String id = (String) result.get("id");
            String title = (String) result.get("title");
            Long price = Long.valueOf(result.get("price").toString());

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });

    }


    //其实就是数学中的约等的思想
    @Test
    public void testLikeQueryIndex() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //封装查询条件对象,范围查询，范围查询包含左右边界,约等号后面就是最大编辑距离取值范围0-2
        SolrQuery query =new SolrQuery("title:ipcona~1");

        //执行查询，传入查询条件，并执行查询得到响应
        QueryResponse queryResponse = solrServer.query(query);

        //解析查询的结果
        SolrDocumentList results = queryResponse.getResults();

        results.forEach(result->{
            String id = (String) result.get("id");
            String title = (String) result.get("title");
            Long price = Long.valueOf(result.get("price").toString());

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });

    }


    //其实就是数学中的约等的思想
    @Test
    public void testSortQueryIndex() throws SolrServerException {
        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //封装查询条件对象,范围查询，范围查询包含左右边界,约等号后面就是最大编辑距离取值范围0-2
        SolrQuery query =new SolrQuery("title:手机");
        query.setSort("price", SolrQuery.ORDER.desc);

        //执行查询，传入查询条件，并执行查询得到响应
        QueryResponse queryResponse = solrServer.query(query);

        //解析查询的结果
        SolrDocumentList results = queryResponse.getResults();

        results.forEach(result->{
            String id = (String) result.get("id");
            String title = (String) result.get("title");
            Long price = Long.valueOf(result.get("price").toString());

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });

    }


    //其实就是数学中的约等的思想
    @Test
    public void testPageQueryIndex() throws SolrServerException {

        int currentPage = 1;

        final int PAGE_SIZE = 3;

        int start = (currentPage-1)*PAGE_SIZE;


        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //封装查询条件对象,范围查询，范围查询包含左右边界,约等号后面就是最大编辑距离取值范围0-2
        SolrQuery query =new SolrQuery("title:手机");

        query.setStart(start);
        query.setRows(PAGE_SIZE);

        //执行查询，传入查询条件，并执行查询得到响应
        QueryResponse queryResponse = solrServer.query(query);

        //解析查询的结果
        SolrDocumentList results = queryResponse.getResults();

        results.forEach(result->{
            String id = (String) result.get("id");
            String title = (String) result.get("title");
            Long price = Long.valueOf(result.get("price").toString());

            System.out.println("id:"+id+" title:"+title+" price:"+price);
        });

    }


    //其实就是数学中的约等的思想
    @Test
    public void testHighLightQueryIndex() throws SolrServerException {


        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");


        //封装查询条件对象
        SolrQuery query =new SolrQuery("title:华为");


        //确定添加高亮字段
        query.addHighlightField("title");
        query.setHighlightSimplePre("<em>");
        query.setHighlightSimplePost("</em>");

        //执行查询，传入查询条件，并执行查询得到响应
        QueryResponse queryResponse = solrServer.query(query);


        //获取高亮查询的结果,双层map的第一层的key为高亮文档的id,第二层map的key为高亮字段，值为高亮字段的高亮结果
        //由于字段可以有多个值，所以api采用了多值设计，如果是单值，集合中只有一个值
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();


        //解析查询的结果
        SolrDocumentList results = queryResponse.getResults();

        results.forEach(result->{
            String id = (String) result.get("id");
            String hightTitle = highlighting.get(id).get("title").get(0);
            String title = (String) result.get("title");
            Long price = Long.valueOf(result.get("price").toString());

            System.out.println("id:"+id+" title:"+hightTitle+" price:"+price);
        });

    }



}
